package cm.tl.pms.util.fileParser.excel;

import cm.tl.pms.common.exception.CustomErrorException;
import cm.tl.pms.util.Token;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;
import java.util.function.Function;

public class TemplateProcessor {

    private static final Map<String, Function<Request, List<ExcelParserByJQ.ExcelParseTarget>>> TEMPLATE_PROCESSORS = new HashMap<>();

    static {
        TEMPLATE_PROCESSORS.put("template_id_3", TemplateProcessor::processTemplateId3);
        TEMPLATE_PROCESSORS.put("template_id_10", TemplateProcessor::processTemplateId3);
        TEMPLATE_PROCESSORS.put("template_id_9", TemplateProcessor::processTemplateId9);
        TEMPLATE_PROCESSORS.put("template_id_8", TemplateProcessor::processTemplateId8);
    }

    public static List<ExcelParserByJQ.ExcelParseTarget> processTemplate(Request req) {
        String templateId = req.getExtractTemplateId();
        Function<Request, List<ExcelParserByJQ.ExcelParseTarget>> processor = TEMPLATE_PROCESSORS.get(templateId);
        if (processor == null) {
            throw new CustomErrorException("暂不支持该模板解析");
        }
        return processor.apply(req);
    }

    private static List<ExcelParserByJQ.ExcelParseTarget> processTemplateId3(Request req) {
        List<ExcelParserByJQ.ExcelParseTarget> targetList = new ArrayList<>();
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB03119JK 资产负债表（接口）", "资  产  总  计", 0, Arrays.asList(2, 3), Arrays.asList("资产总计-期末金额", "资产总计-年初金额")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB03119JK 资产负债表（接口）", "负 债 合 计", 4, Arrays.asList(2, 3), Arrays.asList("负债总额-期末金额", "负债总额-年初金额")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB03119JK 资产负债表（接口）", "流动负债合计", 4, Arrays.asList(2, 3), Arrays.asList("流动负债合计-期末金额", "流动负债合计-年初金额")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB03119JK 资产负债表（接口）", "流动资产合计", 0, Arrays.asList(2, 3), Arrays.asList("流动资产合计-期末金额", "流动资产合计-年初金额")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB03119JK 资产负债表（接口）", "负债和所有者权益总计", 4, Arrays.asList(2, 3), Arrays.asList("负债和所有者权益总计-期末金额", "负债和所有者权益总计-年初金额")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB19003XL 现金流量表", "经营活动产生的现金流量净额", 0, Arrays.asList(2, 3, 4), Arrays.asList("经营净现金流-本月数", "经营净现金流-本年累计", "经营净现金流-上年同期")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB03219JK 利润表（接口）", "四、利润总额（亏损总额以“－”号填列）", 5, Arrays.asList(2, 3, 4), Arrays.asList("利润总额-本月数", "利润总额-本年累计", "利润总额-上年同期")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB03219JK 利润表（接口）", "五、净利润（净亏损以“－”号填列）", 5, Arrays.asList(3, 4), Arrays.asList("净利润-本年累计", "净利润-上年同期")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB03219JK 利润表（接口）", "三、营业利润（亏损以“－”号填列）", 0, Arrays.asList(2, 3, 4), Arrays.asList("营业利润-本月数", "营业利润-本年累计", "营业利润-上年同期")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB03219JK 利润表（接口）", "    其中：营业成本", 0, Arrays.asList(3, 4), Arrays.asList("营业成本-本年累计", "营业成本-上年同期")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB03219JK 利润表（接口）", "    其中：营业收入", 0, Arrays.asList(2, 3, 4), Arrays.asList("营业总收入-本月数", "营业总收入-本年累计", "营业总收入-上年同期")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB03219JK 利润表（接口）", "         销售费用", 0, Arrays.asList(2), Arrays.asList("销售费用-本年累计")));
        return targetList;
    }

    private static List<ExcelParserByJQ.ExcelParseTarget> processTemplateId9(Request req) {
        List<ExcelParserByJQ.ExcelParseTarget> targetList = new ArrayList<>();
        //从12月的财报表取值
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB03219JK 利润表（接口）", "四、利润总额（亏损总额以“－”号填列）", 4, Arrays.asList(2, 3, 4), Arrays.asList("利润总额-本月数", "利润总额-本年累计", "利润总额-上年同期")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB03219JK 利润表（接口）", "    其中：营业收入", 0, Arrays.asList(2, 3), Arrays.asList("营业总收入-本年累计", "营业总收入-上年同期")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB19003XL 现金流量表", "经营活动产生的现金流量净额", 0, Arrays.asList(2, 3, 4), Arrays.asList("经营净现金流-本月数", "经营净现金流-本年累计", "经营净现金流-上年同期")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB03119JK 资产负债表（接口）", "        应收账款", 0, Arrays.asList(2, 3), Arrays.asList("应收账款-期末金额", "应收账款-年初金额")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("CWKB03119JK 资产负债表（接口）", "        存货", 0, Arrays.asList(2, 3), Arrays.asList("存货-期末金额", "存货-年初金额")));
        return targetList;
    }

    private static List<ExcelParserByJQ.ExcelParseTarget> processTemplateId8(Request req) {
        List<ExcelParserByJQ.ExcelParseTarget> targetList = new ArrayList<>();
        //从决算表取值
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("Z02 利润表", "四、利润总额（亏损总额以“－”号填列）", 4, Arrays.asList(2, 3), Arrays.asList("利润总额-本期金额", "利润总额-上期金额")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("Z02 利润表", "    其中：营业收入", 0, Arrays.asList(2, 3), Arrays.asList("营业总收入-本期金额", "营业总收入-上年同期")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("Z03 现金流量表", "经营活动产生的现金流量净额", 0, Arrays.asList(2, 3), Arrays.asList("经营净现金流-本期金额", "经营净现金流-上期金额")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("Z01 资产负债表", "        应收账款", 0, Arrays.asList(2, 3), Arrays.asList("应收账款-期末金额", "应收账款-期初余额")));
        targetList.add(new ExcelParserByJQ.ExcelParseTarget("Z01 资产负债表", "        存货", 0, Arrays.asList(2, 3), Arrays.asList("存货-期末金额", "存货-期初余额")));
        return targetList;
    }

    public static void main(String[] args) {
//        List<ExcelParserByJQ.ExcelParseTarget> targetList = processTemplate(new Request("template_id_3"));
//        System.out.println(targetList);
        for (int i = 0; i < 10; i++) {
            System.out.println(Token.getId());
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Request {
        private String extractTemplateId;
    }

}


